//
//  GoodsDetailViewController.m
//  share_business
//
//  Created by 吴于凌 on 2017/9/7.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "GoodsDetailViewController.h"
#import "JYCarousel.h"
#import "HomeDetailTableViewCell.h"
#import "HomeDetailPictureCell.h"
#import "HomeDetailBuyView.h"
#import "WriteOrderViewController.h"
const static int ad_height = 250 ;
@interface GoodsDetailViewController ()<UITableViewDelegate,UITableViewDataSource,HomeDetailPictureCellDelegate, WriteOrderDelegate>

@property(nonatomic,strong)UITableView *tableView ;
@property (nonatomic,strong) JYCarousel *carouselView ;
@property(nonatomic,assign)NSInteger picHeight ;

@property(nonatomic,strong)UIView * bottomView ;
@property(nonatomic,strong)HomeDetailBuyView *buyView ;
@end

@implementation GoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    NSMutableArray *arr  = [NSMutableArray array];
    [arr addObject:@"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=613150234,3279284455&fm=173&s=384B6AD8CCAB9A473C99AB00030070DC&w=400&h=300&img.GIF"];
    [arr addObject:@"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=613150234,3279284455&fm=173&s=384B6AD8CCAB9A473C99AB00030070DC&w=400&h=300&img.GIF"];
    [arr addObject:@"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=613150234,3279284455&fm=173&s=384B6AD8CCAB9A473C99AB00030070DC&w=400&h=300&img.GIF"];
    [self setHeaderView:arr];
    self.tableView.tableHeaderView = self.carouselView ;
    
    [self setUpBottom];
   
 }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self topLeftItemButton:ImageNamed(@"detail_back") title:nil];
    [self topRightItemButton:ImageNamed(@"shoppingcart") image2:ImageNamed(@"news") title1:nil title2:nil];
    //设置导航栏为透明
    [self transparentNavigationBar];
    UIColor *color = [UIColor blackColor];
    UIButton *leftBtn  = [self getLeftButton];
    UIButton *rightBtn1  = [self getRightButton1];
    UIButton *rightBtn2  = [self getRightButton2];
    CGSize size = CGSizeMake(36, 36);
    leftBtn.size = size ;
    rightBtn1.size = size ;
    rightBtn2.size = size ;
    if (leftBtn) {
        leftBtn.backgroundColor = [color colorWithAlphaComponent:0.5];
        [leftBtn setBorder:0 cornerRadius:18];
    }
    if (rightBtn1) {
         rightBtn1.backgroundColor = [color colorWithAlphaComponent:0.5];
        [rightBtn1 setBorder:0 cornerRadius:18];
    }
    if (rightBtn2) {
         rightBtn2.backgroundColor = [color colorWithAlphaComponent:0.5];
        [rightBtn2 setBorder:0 cornerRadius:18];
    }
}

-(void)initTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 22, APPWIDTH, APPHEIGHT ) style:UITableViewStyleGrouped];
    self.tableView.delegate = self ;
    self.tableView.dataSource = self ;
    //边距
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //  tableview不显示分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //  tableview不显示没内容的cell
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
}
//添加轮播图
- (void)setHeaderView:(NSMutableArray *)array{
    __weak typeof(self) weakSelf = self;
    [self.carouselView removeFromSuperview];
    self.carouselView = nil ;
    self.carouselView = [[JYCarousel alloc] initWithFrame:CGRectMake(0, 0, ViewWidth(self.view), ad_height) configBlock:^JYConfiguration *(JYConfiguration *carouselConfig) {
        //配置指示器类型
        carouselConfig.pageContollType = MiddlePageControl;
        //配置轮播时间间隔
        carouselConfig.interValTime = 3;
        //图片显示方式
        carouselConfig.contentMode = UIViewContentModeScaleAspectFit ;
        //配置动画方向
        carouselConfig.currentPageTintColor = [UIColor orangeColor];
        return carouselConfig;
    } clickBlock:^(NSInteger index) {
        //点击imageView回调方法
        
    }];
    self.carouselView.tag = 101 ;
    //开始轮播
    [self.carouselView startCarouselWithArray:array];
    
    UIImageView *view = [BSWidget commonImageViewWithFrame:CGRectMake(0, ad_height-13, APPWIDTH, 13) image:ImageNamed(@"bg_top_ad_bottom")];
    view.contentMode = UIViewContentModeScaleToFill ;
    
    [self.carouselView addSubview:view];
    
}

#pragma mark tableview事件

//每项数据个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3 ;
}
//每项数据高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 160;
    }
    if (indexPath.row == 2) {
        return self.picHeight ;
    }
    return  50 ;
}
//一共多少项
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1 ;
}

//每一个cell视图
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        static NSString* cellIdentifier = @"HomeDetailTableViewCell";
        HomeDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil ) {
            cell = [[HomeDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            // 点击时候背景不变色
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        }
        return cell ;
    }else if(indexPath.row == 1){
        static NSString* cellIdentifier = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil ) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            // 点击时候背景不变色
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 2, 50)];
            line.backgroundColor = [UIColor orangeColor];
            [cell addSubview:line];
        }
          cell.textLabel.text  = @"商品详情" ;
        
        return cell ;
    }else {
        static NSString* cellIdentifier = @"cell2";
        HomeDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil ) {
            cell = [[HomeDetailPictureCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            // 点击时候背景不变色
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.delegate =self;
        NSArray *picArr = @[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505019071306&di=61437f89d7790359815dc5bf4849ec49&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201409%2F20%2F20140920230643_8tij8.png"];
        [cell setModel:picArr];
        
        return cell ;
    }
  
}
-(void)updatePictureHeight:(NSInteger)headerHeight{
    if (self.picHeight == headerHeight) {
        return ;
    }
    self.picHeight = headerHeight ;
    [self.tableView reloadData];
}


-(void)setUpBottom{
    
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, APPHEIGHT - 55  , APPWIDTH, 55)];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bottomView];
  
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 0, 64, self.bottomView.height);
    [button1 setImage:ImageNamed(@"share") forState:UIControlStateNormal];
    button1.titleLabel.systemFont = 13 ;
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 setTitle:@"分享" forState:UIControlStateNormal];
    [button1 layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:3];
    [button1 setBorder:0.5 cornerRadius:0];
    [button1 addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(button1.maxX, 0, 64, self.bottomView.height);
    [button2 setImage:ImageNamed(@"customer-service") forState:UIControlStateNormal];
    button2.titleLabel.systemFont = 13 ;
        [button2 setBorder:0.5 cornerRadius:0];
      [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 setTitle:@"客服" forState:UIControlStateNormal];
    [button2 layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:3];
    [button2 addTarget:self action:@selector(callAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(button2.maxX, 0, (APPWIDTH-64*2)/2, self.bottomView.height);
      [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button3.titleLabel.systemFont = 13 ;
        [button3 setBorder:0.5 cornerRadius:0];
    [button3 setTitle:@"加入购物车" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(joinAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
        button4.frame = CGRectMake(button3.maxX, 0, (APPWIDTH-64*2)/2, self.bottomView.height);
        [button4 setBorder:0.5 cornerRadius:0];
    button4.backgroundColor = [UIColor orangeColor];
     button4.titleLabel.systemFont = 13 ;
    [button4 setTitle:@"立即购买" forState:UIControlStateNormal];
     [button4 addTarget:self action:@selector(buyAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.bottomView  addSubview:button1];
    [self.bottomView  addSubview:button2];
    [self.bottomView  addSubview:button3];
    [self.bottomView  addSubview:button4];
}

-(void)shareAction{
    
}
-(void)callAction{
    
}
-(void)joinAction{
    self.buyView = [[HomeDetailBuyView alloc]initWithFrame:self.view.frame];
    self.buyView.delegate = self;
    [self.view addSubview:self.buyView];
}
-(void)buyAction{
    self.buyView = [[HomeDetailBuyView alloc]initWithFrame:self.view.frame];
    self.buyView.delegate = self;
    [self.view addSubview:self.buyView];
}
//左边按钮
-(void)leftAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

//填写订单
-(void)WriteOrder{
    WriteOrderViewController *vc = [[WriteOrderViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

}
@end
